package com.lin;

public class Solution34 {

    public int[] searchRange(int[] nums, int target) {
        int leftIndex = binarySearch(nums,target,true);
        int rightIndex = binarySearch(nums,target,false)-1;
        if (leftIndex<=rightIndex&&rightIndex<nums.length&&nums[leftIndex]==target&&nums[rightIndex]==target){
            return new int[]{leftIndex,rightIndex};
        }
        return new int[]{-1,-1};
    }


    public int binarySearch(int[]nums, int target, boolean lower){
        int left = 0;
        int right = nums.length-1;
        int ans = nums.length;
        while (left<=right){
            int mid = (left + right) / 2;
            if(nums[mid] > target || (nums[mid]>=target)&&lower){
                ans = mid;
                right = mid-1;
            }
            else {
                left = mid+1;
            }
        }
        return ans;
    }
}
